package com.qingyan.boot.manage;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.*;
import com.mybatisflex.core.util.StringUtil;
import com.zaxxer.hikari.HikariDataSource;
import io.micrometer.common.util.StringUtils;

import java.util.Scanner;

/**
 * @author csp 2024-04-12
 */
public class Codegen {


    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://192.168.119.120:3306/ry?characterEncoding=utf-8");
        dataSource.setUsername("qingyan");
        dataSource.setPassword("qingyan!@#");

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle1();
        //GlobalConfig globalConfig = createGlobalConfigUseStyle2();
        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);
        globalConfig.setAuthor("qingyan");
        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle1() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置表前缀和只生成哪些表
        globalConfig.setTablePrefix("");
        String module = scanner("请输入模块");
        String tables = scanner("请输入表");
        globalConfig.setGenerateTable(tables.split(","));

        String packageModule = null, resourceModule = null;
        if (StringUtil.isNotBlank(module)) {
            packageModule = "." + module;
            resourceModule = "/" + module;
        }
        //设置生成 mapper
        globalConfig.getMapperConfig().setSourceDir("manage-dao/src/main/java");
        globalConfig.setMapperPackage("com.qingyan.boot.manage.mapper" + packageModule);
        globalConfig.setMapperGenerateEnable(true);
        globalConfig.setMapperXmlPath("manage-dao/src/main/resources/mapper" + resourceModule);
        globalConfig.setMapperXmlGenerateEnable(true);

        // entity
        globalConfig.getEntityConfig().setSourceDir("manage-dao/src/main/java");
        globalConfig.setEntityPackage("com.qingyan.boot.manage.entity" + packageModule);
        //设置生成 entity 并启用 Lombok
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.setEntityWithLombok(true);
        //设置项目的JDK版本，项目的JDK为14及以上时建议设置该项，小于14则可以不设置
        globalConfig.setEntityJdkVersion(17);
        globalConfig.getServiceConfig().buildSuperClassName()
        // service
        globalConfig.getServiceConfig().setSourceDir("manage-service/src/main/java");
        globalConfig.setServicePackage("com.qingyan.boot.manage.service" + packageModule);
        globalConfig.setServiceGenerateEnable(true);
        globalConfig.getServiceImplConfig().setSourceDir("manage-service/src/main/java");
        globalConfig.setServiceImplPackage("com.qingyan.boot.manage.service" + packageModule + ".impl");
        globalConfig.setServiceImplGenerateEnable(true);
        globalConfig.setServicePackage("com.qingyan.boot.manage.biz" + packageModule);
        globalConfig.setServiceGenerateEnable(true);
        globalConfig.setServiceImplPackage("com.qingyan.boot.manage.biz" + packageModule + ".impl");
        globalConfig.setServiceImplGenerateEnable(true);

        // biz
        ServiceConfig bizConfig = new ServiceConfig();
        bizConfig.setSourceDir("manage-service/src/main/java");
        globalConfig.setCustomConfig("bizConfig", bizConfig);
        globalConfig.setCustomConfig("bizConfigPackage", "com.qingyan.boot.manage.biz" + packageModule);
        // bizImpl
        ServiceImplConfig implConfig = new ServiceImplConfig();
        implConfig.setSourceDir("manage-service/src/main/java");
        globalConfig.setCustomConfig("bizConfigImpl", implConfig);
        globalConfig.setCustomConfig("bizConfigImplPackage", "com.qingyan.boot.manage.biz" + packageModule + ".impl");
        //  controller
        globalConfig.getControllerConfig().setSourceDir("manage-web/src/main/java");
        globalConfig.setControllerPackage("com.qingyan.boot.manage.web" + packageModule);
        globalConfig.setControllerGenerateEnable(true);

        //可以单独配置某个列
//        ColumnConfig columnConfig = new ColumnConfig();
//        columnConfig.setColumnName("tenant_id");
//        columnConfig.setLarge(true);
//        columnConfig.setVersion(true);
//        globalConfig.setColumnConfig("tb_account", columnConfig);

        return globalConfig;
    }

    public static GlobalConfig createGlobalConfigUseStyle2() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
        globalConfig.getPackageConfig()
                .setBasePackage("com.test");

        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                .setTablePrefix("tb_")
                .setGenerateTable("tb_account", "tb_account_session");

        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity()
                .setWithLombok(true)
                .setJdkVersion(17);

        //设置生成 mapper
        globalConfig.enableMapper();

        //可以单独配置某个列
        ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setColumnName("tenant_id");
        columnConfig.setLarge(true);
        columnConfig.setVersion(true);
        globalConfig.getStrategyConfig()
                .setColumnConfig("tb_account", columnConfig);

        return globalConfig;
    }

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new RuntimeException("请输入正确的" + tip + "！");
    }
}
